package dgut.party.business.service.impl.pub;

import dgut.party.business.common.util.StringUtils;
import dgut.party.business.dao.publication.PublicationRepository;
import dgut.party.business.dto.to.ActivityPubTO;
import dgut.party.business.dto.to.InformationPubTO;
import dgut.party.business.entity.publication.ActivityPublication;
import dgut.party.business.entity.publication.InformationPublication;
import dgut.party.business.entity.publication.Publication;
import dgut.party.business.service.publication.PublicationService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

@Service
@Log4j2
public class PublicationServiceImpl implements PublicationService {

  @Autowired
  PublicationRepository pubRepo;

  @Override
  public InformationPublication createInformationPub(InformationPubTO dto) {
    assertPubDto(dto);
    Publication pub = Publication.builder().title(dto.getTitle()).content(dto.getContent()).build();

    if (StringUtils.isNotBlank(dto.getSummary())) {
      // 抽取content前五十字作为总结
      Assert.isTrue(dto.getSummary().length() < 50, "公示摘要不可超过50字");
    } else {
      pub.setSummary(dto.getSummary());
    }

    InformationPublication infoPub = InformationPublication.builder().build();
    BeanUtils.copyProperties(pub, infoPub);

    return pubRepo.save(infoPub);
  }

  @Override
  public ActivityPublication createActivityPub(ActivityPubTO dto) {
    assertActPubDto(dto);

    Publication pub = Publication.builder().title(dto.getTitle()).content(dto.getContent()).build();

    if (StringUtils.isNotBlank(dto.getSummary())) {
      // 抽取content前五十字作为总结
      Assert.isTrue(dto.getSummary().length() < 50, "活动摘要不可超过50字");

    } else {
      pub.setSummary(dto.getSummary());
    }

    ActivityPublication actPub = ActivityPublication.builder().build();
    BeanUtils.copyProperties(pub, actPub);

    return pubRepo.save(actPub);
  }

  @Override
  public InformationPublication editInformationPub(InformationPubTO dto) {
    assertPubDto(dto);
    Publication pubExist = pubRepo.findById(dto.getId())
      .orElseThrow(() -> new IllegalArgumentException("该消息公示不存在"));
    Assert.isTrue(pubExist instanceof InformationPublication, "该公示不是消息公示");

    InformationPublication infoPubExist = (InformationPublication) pubExist;
    BeanUtils.copyProperties(dto, infoPubExist);

    return pubRepo.save(infoPubExist);
  }

  @Override
  public ActivityPublication editActivityPub(ActivityPubTO dto) {
    assertActPubDto(dto);
    Publication pubExist = pubRepo.findById(dto.getId())
      .orElseThrow(() -> new IllegalArgumentException("该活动公示不存在"));
    Assert.isTrue(pubExist instanceof ActivityPublication, "该公示不是活动公示");

    ActivityPublication actPubExist = (ActivityPublication) pubExist;
    BeanUtils.copyProperties(dto, actPubExist);

    return pubRepo.save(actPubExist);
  }

  private void assertPubDto(InformationPubTO dto) {
    Assert.notNull(dto, "公示创建参数为空");
    Assert.notNull(dto.getTitle(), "公示标题参数为空");
    Assert.notNull(dto.getContent(), "公示内容为空");
    Assert.isTrue(dto.getContent().length() > 20, "公示内容不可少于20字");
  }

  private void assertActPubDto(ActivityPubTO dto) {
    Assert.notNull(dto, "活动创建参数为空");
    Assert.notNull(dto.getTitle(), "活动标题参数为空");
    Assert.notNull(dto.getContent(), "活动内容为空");
    Assert.isTrue(dto.getContent().length() > 20, "活动内容不可少于20字");
  }
}
